task("xross")
set_category("plugin")

set_menu({
  usage = "xmake xross [platform] [arch]",
  description = "build cross platform",
  options = {
    {'p', "platform", "v", nil, "target platform.\n\tiphoneos|linux|macosx|mingw|android"},
    {nil, "arch", "v", nil, "target arch.\n\tiphoneos: armv7|armv7s|arm64|i386|x86_64\n\tlinux: arm64|x86_64\n\tmacosx: arm64|x86_64\n\tmingw: arm64|x86_64\n\tandroid: arm64-v8a|armeabi-v7a|x86_64"},
    {'a', "archive", "k", nil, "xcode archive"}
  }
})

local pari = {
  iphoneos = { "armv7", "armv7s", "arm64", "i386", "x86_64" },
  watchos = { "arm64_32", "armv7k" },
  appletvos = { "arm64" },
  iphonesimulator = { "arm64", "x86_64" },
  watchsimulator = { "arm64", "x86_64" },
  appletvsimulator = { "arm64", "x86_64" },
  linux = { "arm64", "x86_64" },
  macosx = { "arm64", "x86_64" },
  mingw = {  "arm64", "x86_64" },
  android = { "armeabi", "arm64-v8a", "armeabi-v7a", "x86_64", "x86", } -- "mips", "mips64"
}


local function indexOf(array, value)
  for i, v in ipairs(array) do
      if v == value then
          return i  
      end
  end
  return nil
end

local function printTable(t, indent)
  indent = indent or 0
  local indentStr = string.rep("  ", indent)
  
  if type(t) ~= "table" then
      print(indentStr .. tostring(t))
      return
  end

  print(indentStr .. "{")
  for k, v in pairs(t) do
      local keyStr = tostring(k)
      if type(v) == "table" then
          print(indentStr .. "  " .. keyStr .. " = ")
          printTable(v, indent + 1)
      else
          local valueStr = tostring(v)
          print(indentStr .. "  " .. keyStr .. " = " .. valueStr)
      end
  end
  print(indentStr .. "}")
end

local function xmakeBuild(platf, archf, execvFn)
  if platf and archf then
    print("🔥 ".. platf .. "🌹 " .. archf)
  end
  local config = { "f" }
  if platf then
    table.insert(config, "-p")
    table.insert(config, platf)
  end
  if archf then
    table.insert(config, "-a")
    table.insert(config, archf)
  end
  print("⛰️ xmakeBuild: " .. table.concat(config, ", "))
  printTable(os)
  printTable(sudo)
  execvFn("xmake", config)
  execvFn("xmake", { "package", "-P", "."}) -- "--verbose"
end

local function buildForPlatformArch(platf, archf, execvFn)
  local hasTarget = false
  local archs = pari[platf]
  if archs then
    if archf then
      if indexOf(archs, archf) then
        xmakeBuild(platf, archf, execvFn)
        hasTarget = true
      end
    else
      for _, arch in ipairs(archs) do
        xmakeBuild(platf, arch, execvFn)
      end
      hasTarget = true
    end
  end
  if not hasTarget then
    platf = platf or "Unknown platform"
    archf = archf or "Unknown architecture"
    print("🍄" .. platf .. "-".. archf .. " not supported!") 
  end
end

on_run(function()
  import("core.base.option")
  local platf = option.get("platform")
  local archf = option.get("arch")
  printTable(os)
  if platf or arch then
    buildForPlatformArch(platf, archf, os.execv) -- 非 script lifecyle hook scope, 函数无法获取到 os 模块的扩展函数
  else
    xmakeBuild(nil, nil, os.execv)
  end
end)
